package com.ruoyi.base.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ruoyi.base.domain.SMenuFinereport;
import com.ruoyi.base.service.ISMenuFinereportService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author fuzhen-pc
 * @description TODO
 * @date 2023/7/13 8:56
 */
@RestController
@RequestMapping("/base/sMenuFinereport")
public class SMenuFinereportController extends BaseController {
    @Autowired
    ISMenuFinereportService menuFinereportService;

    /**
     * 新增模板
     * @param menuFinereport
     * @return
     */
    @PostMapping
    @Log(title = "新增单证模板", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody SMenuFinereport menuFinereport){
        return menuFinereportService.save(menuFinereport)==true?AjaxResult.success():AjaxResult.error();
    }
    
    /**
     * 修改模板
     * @param menuFinereport
     * @return
     */
    @PostMapping("/update")
    @Log(title = "修改单证模板", businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody SMenuFinereport menuFinereport){
        UpdateWrapper<SMenuFinereport> wrapper = new UpdateWrapper<>();
        wrapper.eq("ID",menuFinereport.getId());
        wrapper.eq("DEL_FLAG","0");
        wrapper.set("MENU_NAME",menuFinereport.getMenuName());
        wrapper.set("MODULE_NAME",menuFinereport.getModuleName());
        wrapper.set("MODULE_URL",menuFinereport.getModuleUrl());
        wrapper.set(StringUtils.isNotEmpty(menuFinereport.getCompanyCod()),"COMPANY_COD",menuFinereport.getCompanyCod());
        wrapper.set(menuFinereport.getSort()!=null,"SORT",menuFinereport.getSort());
        wrapper.set("UPDATE_BY", SecurityUtils.getUsername());
        wrapper.set("UPDATE_TIME", DateUtils.getNowDate());
        return menuFinereportService.update(wrapper)==true?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 获取模板
     * @return
     */
    @GetMapping
    public AjaxResult getList(SMenuFinereport menuFinereport){

       QueryWrapper<SMenuFinereport> queryWrapper = new QueryWrapper<>();

        queryWrapper.lambda().eq(StringUtils.isNotEmpty(menuFinereport.getMenuName()),SMenuFinereport::getMenuName,menuFinereport.getMenuName());
        queryWrapper.lambda().eq(StringUtils.isNotEmpty(menuFinereport.getCompanyCod()),SMenuFinereport::getCompanyCod,menuFinereport.getCompanyCod());
        queryWrapper.lambda().like(StringUtils.isNotEmpty(menuFinereport.getModuleName()),SMenuFinereport::getModuleName,menuFinereport.getModuleName());
        queryWrapper.lambda().orderByAsc(SMenuFinereport::getSort);
        queryWrapper.lambda().orderByAsc(SMenuFinereport::getCreateTime);

        List<SMenuFinereport> list = menuFinereportService.list(queryWrapper);

        return AjaxResult.success(list);

    }

    /**
     * 删除报表模板
     */
    @PostMapping("/del/{id}")
    @Log(title = "删除单证模板", businessType = BusinessType.DELETE)
    public AjaxResult deleteMenuFineReport(@PathVariable("id")Long id){
        return menuFinereportService.removeById(id)==true?AjaxResult.success():AjaxResult.error();
    }

    /**
     * 批量删除报表模板
     */
    @PostMapping("/del/selectIds")
    @Log(title = "批量删除单证模板", businessType = BusinessType.DELETE)
    public AjaxResult deleteMenuFineReports(@RequestBody List<Long> selectIds){
        return menuFinereportService.removeByIds(selectIds)==true?AjaxResult.success():AjaxResult.error();
    }

}
